package com.edu.whu.controller;

import com.edu.whu.entity.UserInfo;
import com.edu.whu.entity.UserInfoDto;
import com.edu.whu.service.ServiceException;
import com.edu.whu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/register")
    public Map<String, String> register(@RequestBody UserInfo info) throws ServiceException {
        String token = userService.createUser(info);
        Map<String, String> response = new HashMap<>(1);
        response.put("token", token);
        return response;
    }

    @PostMapping("/login")
    public Map<String, String> login(@RequestBody UserInfo userInfo) throws ServiceException {
        String token = userService.login(userInfo);
        Map<String, String> response = new HashMap<>(1);
        response.put("token", token);
        return response;
    }

    @GetMapping("/{id}")
    public UserInfoDto getUserInfo(@PathVariable String id) {
        return userService.getUser(id);
    }
}
